import java.util.StringTokenizer;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.FileReader;
import java.util.regex.*;
public class CorreosConStringTokenizer{
		static int maxcadenas=4;
	public static void main(String[] args) throws IOException {
		BufferedReader in = null;
		PrintWriter out = null;
		PrintWriter out2 = null;
		boolean nomail=false;

		try{
			in = new BufferedReader(new FileReader("direccionesdecorreo.txt"));
			out = new PrintWriter(new FileWriter("final-validos.txt"));
			out2 = new PrintWriter(new FileWriter("final-invalidos.txt"));

			String s;
			while((s = in.readLine()) != null){
				
				StringTokenizer cad1 = new StringTokenizer(s,"@");
				StringBuffer salida = new StringBuffer("");
				
				StringBuffer[] cadena;
				cadena = new StringBuffer[maxcadenas];
				for(int i=0;i<maxcadenas;i++){
					cadena[i] = new StringBuffer();
				}
				int cont=0;

				Pattern p=Pattern.compile("^[^a-zA-Z]");
				Matcher m=p.matcher(s);
				if (m.find()){
					System.out.println("\ntratamos "+s);
					System.out.println("No es un e-mail");
					out2.println(s);
					continue;
				}
				else{
					System.out.println("\ntratamos "+s);
					p=Pattern.compile( "[^A-Za-z0-9\\.\\@_\\-#]+");
					m =p.matcher(s);
					if(m.find()){
						System.out.println("No es un e-mail,tiene caracteres invalidos");
						out2.println(s);
						continue;
					}
					p=Pattern.compile("^www\\.");
					m=p.matcher(s);
					if(m.find()){
						System.out.println("No es un e-mail,no puede empezar por www.");
						out2.println(s);
						continue;
					}
				}
				
				while (cad1.hasMoreTokens() ){ // aki partimos en 2 la linea, con el separador de @
					if(cont>=2){
						nomail=true;
						break;
					}
					String palabra = cad1.nextToken();
					cadena[cont].append(palabra);
					cont++;
				}
				if(nomail || cont<2){
					System.out.println("La linea: "+s+" no es un email (@)");
					out2.println(s);
				}
				else{
					String cadenastring = new String(cadena[1]);
					StringTokenizer cad2 = new StringTokenizer(cadenastring,".");
					Pattern q=Pattern.compile("^[^a-zA-Z]");
					Matcher n=q.matcher(cadena[1]);
					if(m.find()){
						System.out.println("No es un e-mail");
						out2.println(s);
						continue;
					}
					else{
						nomail=false;
						cont=0;
						while (cad2.hasMoreTokens()){
							if(cont>=2){
								nomail=true;
								break;
							}
							String palabra2 = cad2.nextToken();
							cadena[cont+2].append(palabra2);
							cont++;						
						}
						if(nomail || cont<2){
							System.out.println("La linea: "+s+" no es un email (.)");
							out2.println(s);
						}
						else{
							System.out.println("La linea: "+s+" SI es un email!!");
							out.println(s);
						}
					}
				}
			}
		} finally {
			if (in != null){
				in.close();
			}
			if (out != null){
				out.close();
			}
			if (out2 != null){
				out2.close();
			}
		}
	}
}
